package com.ksm.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ksm.domain.dao.TauxDao;
import com.ksm.domain.dao.document.type.DocumentTypeDao;
import com.ksm.domain.entity.DocumentType;
import com.ksm.domain.entity.Taux;
import com.ksm.domain.entity.Taux.TypeTaux;
import com.ksm.transfer.dto.TvaDTO;
import com.ksm.transfer.dto.TypeDocumentDTO;

@Service
public class ReferentielService implements IReferentielService {

	@Autowired
	TauxDao tauxDao;

	@Autowired
	DocumentTypeDao documentTypeDao;

	private static Logger LOGGER = Logger.getLogger(ReferentielService.class);

	public List<TvaDTO> loadTva() {
		LOGGER.info("Loading TVA rate");
		List<TvaDTO> tvaDTOs = new ArrayList<TvaDTO>();
		for (Taux aTaux : tauxDao.findByType(TypeTaux.TVA)) {
			tvaDTOs.add(new TvaDTO(aTaux.getId(), aTaux.getLibelle(), aTaux.getTaux()));
		}

		return tvaDTOs;
	}

	@Override
	public List<TypeDocumentDTO> loadTypeDocument() {
		LOGGER.info("Loading TVA rate");
		List<TypeDocumentDTO> typeDocumentDTOs = new ArrayList<TypeDocumentDTO>();
		for (DocumentType adDocumentType : documentTypeDao.findAll()) {
			typeDocumentDTOs.add(new TypeDocumentDTO(adDocumentType.getId(), adDocumentType.getLibelle()));
		}

		return typeDocumentDTOs;
	}
}